package main

import (
	"github.com/PuerkitoBio/goquery"
	"github.com/gookit/color"
	"go-movie/model"
	"go-movie/parse"
	"net/http"
	"strings"
)

var (
	BaseUrl = "https://movie.douban.com/top250"
)



//开始爬取
func Start() {
	var movies []parse.DoubanMovie

	pages := parse.GetPages(BaseUrl)
	for _, page := range pages {
		doc, err := parsePerPage(strings.Join([]string{BaseUrl, page.Url}, ""))
		if err != nil {
			color.Error.Print(err)
		}

		movies = append(movies, parse.ParseMovies(doc)...)

	}

	model.Add(movies)
}

//
func parsePerPage(url string) (*goquery.Document, error){
	resp, err := http.Get(url)
	if err != nil {
		color.Error.Print(err)
	}

	defer resp.Body.Close()

	document, err := goquery.NewDocumentFromReader(resp.Body)
	if err != nil {
		color.Error.Print(err)
		return nil, err
	}

	return document,nil
}

func main() {
	model.Setup()
	Start()
	//defer model.CloseDB()
}